Pui.Search = {
    init: function () {
        if (document.getElementById('SearchForm')) {
            Pui.searching.init();
            Pui.header.init();
            //this.crossDomain();
        }
        Pui.searching.Searched.init();
        this.keywords_align();
        Pui.ui.tabTurn('.search_series dl dd a', '.series_lists', 'ac');
        if (document.getElementById('B-Searched-VideoPlay') && document.getElementsByName('Videoplay')) {
            this.thumbHighLight();

        }
		this.vedioFold();
        this.sosorank();
        this.SearTab();
        Pui.ui.tabTurn('.searchrank .tab li', '.searchrank table', 'over');
    },
    crossDomain: function () {
        document.domain = 'pomoho.com';
        $('<iframe style="position:absolute;z-index:-1;border:none;width:0;height:0;" id="autoCompleteShow" name="autoCompleteShow" src="http://search.pomoho.com/autoComplete.html"></iframe>').appendTo($('.header1 .header_main'));
    },
    keywords_align: function () {
        var ul = $('.seach_related ul');
        var ulno = ul.length;
        var lino = ul.eq(0).find('li').length;
        var key_arr = new Array();
        for (var j = 0; j < lino; j++) {
            for (var i = 0; i < ulno; i++) {
                var li_width = ul.eq(i).find('li').eq(j).width();
                key_arr.push(li_width);
            }
            function sortNum(a, b) {
                return b - a;
            }
            var a = key_arr.slice(j * ulno).sort(sortNum);
            for (var i = 0; i < ulno; i++) { ul.eq(i).find('li').eq(j).css('width', a[0]); }
        }
    },
    emotion: function (index) {
        var emotionID = index + 1;
        var rowN = $('#VideoBox .row').length;
        $('#Videoroll .emotion').find('.l').click(function () {
            if (emotionID >= 2) {
                $('#VideoBox').animate({ left: -(600 * (emotionID - 2)) }, 500);
                if (emotionID == 2) {
                    $('#Videoroll .l').removeClass('l_b').attr('title', '左边没有了');
                } else if (emotionID == rowN) {
                    $('#Videoroll .r').addClass('r_b').attr('title', '向右翻');
                }
                emotionID--;
            }
            this.blur();
            return false;
        }).end().find('.r').click(function () {
            if (emotionID <= (rowN - 1)) {
                $('#VideoBox').animate({ left: -(600 * emotionID) }, 500);
                if (emotionID == 1) {
                    $('#Videoroll .l').addClass('l_b').attr('title', '向左翻');
                } else if (emotionID == (rowN - 1)) {
                    $('#Videoroll .r').removeClass('r_b').attr('title', '右边没有了');
                }
                emotionID++;
            }
            this.blur();
            return false;
        });
    },
    thumbHighLight: function () {
        //var s_w=window.location.toString();
        var videoBox = $('.playlist #VideoBox'), _self = this, rowN = $('.playlist #VideoBox .row').length;
        var video_id = videoid;
        var v_ww = 'current_' + video_id;

        var videoBox = $('#VideoBox'), _self = this, rowN = $('#VideoBox .row').length;
        videoBox.find('.row ul').each(function (i) {
            var t = $(this);
            var v_vw = t.attr('id');
            if (v_vw == v_ww) {
                t.addClass('ac');
                var index = videoBox.find('.row').index(t.parent()[0]);
                videoBox.animate({ left: -(600 * index) }, 500);
                if (index > 0) { $('#Videoroll .l').addClass('l_b').attr('title', '向左翻'); }
                else if (index == rowN - 1) { $('#Videoroll .r').removeClass('r_b').attr('title', '右边没有了'); }
                _self.emotion(index);
            }
        });
    },
    sosorank: function () {
        var o_li = $('.soso_sort .tab li');
        o_li.click(function () {
            var _self = $(this);
            if (_self.parent().children().length <= 1) return false;
            _self.addClass('over').siblings().removeClass('over');
            var index = _self.parent().children().index(_self[0]);
            _self.parents('.soso_sort').find('.topList').css('display', 'none').eq(index).css('display', 'block');
            return false;
        });
    },
    SearTab: function () {
        var taba = $('.SearTab .a1');
        var tabb = $('.SearTab .a2');
        var nena = $('.search_show .xiangg');
        var nenb = $('.search_show .tuijian');
        taba.click(function () {
            taba.addClass('ac');
            tabb.removeClass('ac');
            nena.addClass('ac');
            nenb.removeClass('ac');
        });
        tabb.click(function () {
            tabb.addClass('ac');
            taba.removeClass('ac');
            nenb.addClass('ac');
            nena.removeClass('ac');
        });
    },
    resizeImg: function () {
        //var obj = document.getElementById(obj); 
        var obj = document.getElementsByName('card_a');
        var objContent = obj.innerHTML;
        var imgs = obj.getElementsByTagName('img');
        if (imgs == null) return;
        for (var i = 0; i < imgs.length; i++) {
            if (imgs[i].height > parseInt(obj.style.height)) {
                //imgs[i].width = parseInt(obj.style.width);
                imgs[i].height = parseInt(obj.style.height);
            }
        }
    },
    vedioFold: function(){
	   $(function(){
		 var margin = $('.mvList:first').css('margin-bottom');
		 var firstH = $('.mvList:first').height()+ parseInt(margin);
		 var allH=0; var HH;
		 var mvList = $('.mvList');
		 $('.mvInfo').css('height',firstH);
		 for(i=0;i<mvList.length;i++){
			 HH = mvList.eq(i).height() + parseInt(margin);
			 allH+=HH;
		 }
	     $('.foldMore').toggle(function(){
		    $(this).addClass('folded');
			$('.mvInfo').animate({height:allH},800);
			$(this).text('收起更多');
		 },function(){
		    $(this).removeClass('folded');
			$('.mvInfo').animate({height:firstH},800);
			$(this).text('查看更多');
		 });
	   });
	}
}
$(function () { Pui.Search.init(); });

Pui.soIndex = {
    init: function(){
	  this.ItemChange();
	  $('#soIndexSearchForm').submit(function () {
            Pui.soIndex.DoSearch(this);
            return false;
         });
	},
	ItemChange: function(){
    var yuanj = $('<b class="lt"></b><b class="rt"></b>');
	$('body.searchIndex #Item a').click(function(){
	$(this).find('lt').remove();
	$(this).find('rt').remove();
	$(this).addClass('ac').append(yuanj);
	$(this).siblings().removeClass('ac');
	if( $(this).hasClass('sVideo') ){ $('#SearchType').val('video'); }
	else if( $(this).hasClass('sPic') ){ $('#SearchType').val('image'); }
	else if( $(this).hasClass('sBlog') ){ $('#SearchType').val('blog'); }
	});
	},
	DoSearch: function(o){
	  var val = $('body.searchIndex').find('#SearchInput').val();
	  var url;
	  if ($.trim(val) == '' || val == '搜一下') {
            alert('对不起，关键词不能为空，请重新输入');
            $('#SearchInput').focus();
            return false;
        } else {
		var sdi = o.searchDomain;
            var sd = "http://so.baomihua.com";
            if (sdi != undefined && sdi.value != "") {
                sd = sdi.value;
            }
		var key = val;
			if ($('#SearchType').val() == 'blog'){
                url = sd + "/b/" + key + '.html';
			}else if($('#SearchType').val() == 'image'){
				url = sd + "/img/" + key + '.html';
			}else if($('#SearchType').val() == 'video')
			{ 
			    url = sd + "/v/" + key + '_p0s04.html';
			}
		if (document.location.hostname.indexOf("so.baomihua.com") != -1) {
                document.location.href = url;
            } else {
                window.open(url, '_self');
            }
		return false;
		}
	}

}
/*搜索结果页TAB*/
function changeShow(objID) {
    if (objID == "div_video_list") {
        document.getElementById("menuVideo").className = "a2";
        document.getElementById("menuMovie").className = "a1";
        document.getElementById("div_video_list").style.display = "block";
        document.getElementById("div_series_cusubject").style.display = "none";
    }
    else if (objID == "div_series_cusubject") {
        document.getElementById("menuVideo").className = "a1";
        document.getElementById("menuMovie").className = "a2";
        document.getElementById("div_video_list").style.display = "none";
        document.getElementById("div_series_cusubject").style.display = "block";
    }
}
function ClickVideo(videoID, videoName, grabUrl, keyWord, videoLink) {
    var param = { objType: 3, objID: videoID,
        objName: videoName, objImgUrl: grabUrl,
        objIntro: '', objLinkUrl: videoLink, cacheType: 6, RelateObj: keyWord
    };
    $.ajax({
        type: "get",
        url: "http://www.pomoho.com/api/userapi.asmx/GetActionX",
        data: param,
        dataType: "jsonp",
        jsonp: "jsoncallback",
        timeout: 300000,
        success: function (result) {
            //alert(result.Result);
        }
    });
}
searchVideoNew = {
    init: function () {
        var key = $("#SearchInput").val();
        $.getJSON("http://so.baomihua.com/interfaces/getsearchaction.aspx?jsoncallback=?&count=10&key=" + encodeURIComponent(key), function (data) {
            if (data.html != "") {
                $("#div_searchclickvideo_new").html(unescape(data.html));
            }
        });

    }
}